package com.igeek.d0531;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Objects;

/**
 * @Author: zhiyu
 * @Date:17:08
 * @DESC:
 */
public class ArrayListDemo {
    public static void main(String[] args) {
        getPeople();
        System.out.println("removeRepeat(getPeople()) = " + removeRepeat(getPeople()));
        String str = "ab3y8u9";
        System.out.println("stringAdd(str) = " + stringAdd(str));
        String str1 = "9 10 12 6 8 16";
        System.out.println("splitAndCompare(str1) = " + splitAndCompare(str1));
    }
    public static ArrayList<Person> getPeople(){
        Person person1 = new Person("a", 23);
        Person person2 = new Person("b", 22);
        Person person3 = new Person("c", 21);
        Person person4 = new Person("d", 23);
        ArrayList<Person> people = new ArrayList<>();
        people.add(person1);
        people.add(person2);
        people.add(person3);
        people.add(person4);
        people.add(person1);
        return people;
    }
    public static HashSet<Person> removeRepeat(ArrayList<Person> people){
        HashSet<Person> people1 = new HashSet<>();
        for (Person p:people) {
            people1.add(p);
        }
        return people1;
    }
    public static int stringAdd(String str){
        ArrayList<String> strings = new ArrayList<>();
        int result = 0;
        for (int i = 0; i < str.length(); i++) {
            String s = str.substring(i, i + 1);
            if (s.matches("\\d")){
                result+=Integer.parseInt(s);
            }
            strings.add(s);
        }
        return result;
    }
    public static ArrayList<Integer> splitAndCompare(String str){
        String[] split = str.split(" +");
        ArrayList<Integer> arr = new ArrayList<>();
        for (String i:split) {
            arr.add(Integer.parseInt(i));
        }
        arr.sort(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });
        return arr;
    }
}
